<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'home';
$return['ischecklogin']=cfc('core')->checklogin('','',true);
$return['pluginset']['ailogo']=tomedia($return['pluginset']['ailogo']);
$return['basic']=$return['pluginset'];
if($_RQ['op']=='home'){
	$id=intval($_RQ['id']);
	$workerid=intval($_RQ['workerid']);
	$groupid=intval($_RQ['groupid']);
	$goodsid=intval($_RQ['goodsid']);
	if(!$id && !$workerid){
		$workerid=$this->model('kefu')->gettcpworkerid($groupid);
	}
	if(!$goodsid && $_RQ['isgoods'] && $_SESSION['goodsid']){
		$goodsid=$_SESSION['goodsid'];
	}
	if($goodsid){
		$return['goods']=pdo_get('jy_weishop_goods',array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']),array('id','title','price','image','subtitle'));
	}
	if(!$id){
		$item=pdo_get('jy_aihire_tcp',array('status'=>array(1,2),'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'workerid'=>$workerid),'','createtime desc');
		if($item['id']){
			$lasttcplog=pdo_get('jy_aihire_tcplog',array('tcpid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'','createtime desc');
			if($item['createtime']<=TIMESTAMP-12*3600 && $lasttcplog['createtime']<=TIMESTAMP-12*3600){
				pdo_update('jy_aihire_tcp',array('status'=>3,'complete_time'=>time()),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				$item['id']=0;
			}
		}
		if($workerid){
			$worker=pdo_get('jy_aihire_worker',array('id'=>$workerid,'uniacid'=>$_SESSION['uniacid']));
		}
		if(!$item['id']){
			$item=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'status'=>1,
				'workerid'=>$workerid,
				'groupid'=>$worker['groupid']?:$groupid,
				'goodsinfo'=>'免费在线咨询',
				'ordersn'=>get_ordersn('jy_aihire_tcp','ordersn'),
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_aihire_tcp',$item);
			$item['id']=pdo_insertid();
		}
	}else{
		$item=pdo_get('jy_aihire_tcp',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		$worker=pdo_get('jy_aihire_worker',array('id'=>$item['workerid'],'uniacid'=>$_SESSION['uniacid']));
	}
	if(!$item['id']){
		exi('当前咨询，不存在对应的客服','error','/index/');
	}
	if($item['mid']!=$_SESSION['uid']){
		exi('该对话不是您的！','error','/index/');
	}
	if($worker['groupid']){
		$return['fastmsg']=pdo_getall('jy_aihire_fastmsg',array('uniacid'=>$_SESSION['uniacid'],'groupid'=>$worker['groupid'],'status'=>1,'type'=>0),array('id','title'));
	}else{
		$return['fastmsg']=pdo_getall('jy_aihire_fastmsg',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>0),array('id','title'));
	}
	$worker['title']=$worker['title']?:$this->setting['aititle'];
	$worker['logo']=$worker['logo']?:$this->setting['ailogo'];
	$worker['logo']=tomedia($worker['logo']);
	$worker['onlinetime']=json_decode($worker['onlinetime'],true);
	$worker['tags']=json_decode($worker['tags'],true);
	$worker['api_data']=json_decode($worker['api_data'],true);
	$worker['salenum']=pdo_count('jy_aihire_tcp',array('uniacid'=>$_SESSION['uniacid'],'workerid'=>$item['workerid']));
	if(!$worker['fee_type']){
		$worker['fee_type']=$this->setting['fee_type'];
		$worker['fee_dui']=$this->setting['fee_dui'];
		$worker['fee_ju']=$this->setting['fee_ju'];
		$worker['fee_jubi']=$worker['fee_ju']*$this->setting['jifen_rate'];
	}
	$item['worker']=$worker;
	$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
	$item['statustitle']=$this->tcp_status[$item['status']];
	$member=cfc('core')->member_show($_SESSION['uid']);
	$return['openid']= $_SESSION['openid'];
	$return['member']= $member;
	$return['item']= $item;
	$return['list']= array();
	$return['post']= array('msg'=>'','tcpid'=>$item['id'],'workerid'=>$item['workerid'],'star'=>5);
	$return['params']= array('tcpid'=>$item['id']);
	set_html('title','对话记录');
	exi($return);
}
if($_RQ['op']=='list'){
	$tcpid=$_RQ['tcpid'];
	$params=array('uniacid'=>$_SESSION['uniacid'],'tcpid'=>$tcpid);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total = 0;
	$list=pdo_getslice('jy_aihire_tcplog',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$chatlogtime = time();
	if($list){
		foreach($list as $k=>&$v){
			$settime = $v['createtime'];
			if(($chatlogtime - $v['createtime']) > 600 || $k+1==count($list)){
				$v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
			}else{
				$v['createtime'] = '';
			}
			$chatlogtime = $settime;
			$v['msg'] = cfc('tools')->filterword($v['msg'],$setting['filterword']);
			$v['msg'] = preg_replace('/\xEE[\x80-\xBF][\x80-\xBF]|\xEF[\x81-\x83][\x80-\xBF]/', '[无法识别字符]', $v['msg']);
			$hrefhtml = '@(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))@';
			preg_match_all($hrefhtml,$v['msg'],$resarray);  
			if($v['type'] == 1 || $v['type'] == 2){
						foreach($resarray[0] as &$vv){
							if(!empty($vv)){
								$v['msg'] = str_replace($vv,"<a href='".$vv."'>".$vv."</a>",$v['msg']);
							}
						}
			}
			if(!$_SESSION['wxapp_id'] || $_SESSION['fromh5wxapp']){
				$v['msg'] = str_replace("\n","</br>",$v['msg']);
			}
			$v['fileinfo']=json_decode($v['fileinfo'],true);
			$v['goods']=json_decode($v['goods'],true);
		}
	}
	$return['list']= $list;
	exi($return);
}
if($_RQ['op']=='gethtml'){
	$id=intval($_RQ['id']);
	$chatid=intval($_RQ['chatid']);
	$getmemberlogo=$this->session('getmemberlogo');
	$memberlogo=$this->session('memberlogo');
	$info=pdo_get('jy_aihire_tcplog',array('uniacid'=>$_SESSION['uniacid'],'chatid'=>$chatid,'id'=>$id));
	if($_SESSION['os']=='windows'){
		$htmltime = '<div class="message_box_time">'.$info['createtime'].'</div>';
		if($info['type'] == 1 || true){
			$html .= $htmltime;
			if($info['setmid'] != $_SESSION['uid']){
					$html .='<div class="message_box"><img class="mssage_box_pic" src="'.$getmemberlogo.'" style="display:inline-block;"/><div class="message_content"><div class="message_content_inner"><p>'.$info['answer'].'</p></div></div></div>';
			}else{
					$html .='<div class="message_box from_self"><img class="mssage_box_pic" src="'.$memberlogo.'" style="display:inline-block;"/><div class="message_content"><div class="message_content_inner"><p>'.$info['askmsg'].'</p></div></div></div>';
			}			
		}
	}else{
		if($info['type'] == 1 || true){
			if($info['setmid'] == $_SESSION['uid']){
				$html .='<div class="r clearfix"><img class="pic"/><p class="text">'.$info['askmsg'].'</p></div>';
			}else{
				$html .='<div class="l clearfix"><img class="pic"/><p class="text">'.$info['answer'].'</p></div>';
			}
			$html .= $htmltime;
		}
	}
	$return['html']= $html;
	exi($return);
}
if($_RQ['op']=='addlog'){
	$postdata=get_postdata();
	$res=$this->model('kefu')->addchatlog($postdata);
	exi($res);
}
if($_RQ['op']=='getmsg'){
	$workerid = trim($_RQ['workerid']);
	$tcpid = trim($_RQ['tcpid']);
	$chatmsg = trim($_RQ['msg']);
	$getmsg=$this->model('model')->getmsg($chatmsg,'',$workerid);
	if($getmsg && $getmsg!='fail'){
		$postdata=array(
			'tcpid'=>$tcpid,
			'msg'=>$getmsg,
			'type'=>0,
			'workerid'=>$workerid,
			'is_worker'=>1,
		);
		$res=$this->model('kefu')->addchatlog($postdata);
	}else{
		/*$res['log']=array(
			'id'=>$logid,
			'mid'=>$_SESSION['uid'],
			'type'=>0,
			'tcpid'=>$tcpid,
			'msg'=>'机器人设置异常，无法自动回复！',
			'is_worker'=>1,
		);*/
	}
	exi($res);
}
if($_RQ['op']=='getnewlog'){
	$tcpid = trim($_RQ['tcpid']);
	$is_worker = intval($_RQ['is_worker']);
	$lastid = intval($_RQ['lastid']);
	$log = pdo_get('jy_aihire_tcplog',array('id >'=>$lastid,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'tcpid'=>$tcpid,'is_worker'=>$is_worker));
	if($log){
			$chatlogtime = time();
			$settime = $log['createtime'];
			$log['createtime'] = '';
			$log['msg'] = cfc('tools')->filterword($log['msg'],$setting['filterword']);
			$log['msg'] = preg_replace('/\xEE[\x80-\xBF][\x80-\xBF]|\xEF[\x81-\x83][\x80-\xBF]/', '[无法识别字符]', $log['msg']);
			$hrefhtml = '@(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))@';
			preg_match_all($hrefhtml,$log['msg'],$resarray);  
			if(!$_SESSION['wxapp_id'] || $_SESSION['fromh5wxapp']){
				$log['msg'] = str_replace("\n","</br>",$log['msg']);
			}
			$log['fileinfo']=json_decode($log['fileinfo'],true);
			$log['goods']=json_decode($log['goods'],true);
			$res['log']=$log;
	}else{
		$res=array('log'=>false);
		if($this->setting['aitime']){
			$log = pdo_get('jy_aihire_tcplog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'tcpid'=>$tcpid,'is_worker'=>0),'','createtime desc');
			$log2 = pdo_get('jy_aihire_tcplog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'tcpid'=>$tcpid,'is_worker'=>1),'','createtime desc');
			$log2['createtime']=$log2['createtime']?:0;
			if($log['createtime']>$log2['createtime'] && $log['createtime'] && $log['createtime']<time()-$this->setting['aitime']){
				$getmsg=$this->model('model')->damsg($log['msg'],2);
				if($getmsg && $getmsg!='fail'){
					$postdata=array(
						'tcpid'=>$tcpid,
						'msg'=>$getmsg,
						'type'=>0,
						'workerid'=>$log['workerid'],
						'is_worker'=>1,
					);
					$res=$this->model('kefu')->addchatlog($postdata);
				}
			}
		}
	}
	exi($res);
}